package io.zhiller.storagedemo

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import io.zhiller.storagedemo.storage.domain.Note
import io.zhiller.storagedemo.storage.vm.NoteVM
import io.zhiller.storagedemo.ui.theme.StorageDemoTheme
import kotlinx.coroutines.launch
import org.koin.androidx.compose.koinViewModel

class MainActivity : ComponentActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    enableEdgeToEdge()
    setContent {
      StorageDemoTheme {
        Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
          innerPadding.calculateTopPadding()
          NoteComp()
        }
      }
    }
  }
}

@Composable
fun NoteComp(
  noteVM: NoteVM = koinViewModel<NoteVM>()
) {
  val notes = noteVM.state.collectAsState()
  val coroutineScope = rememberCoroutineScope()
  Box(
    modifier = Modifier
      .fillMaxSize()
      .padding(24.dp)
  ) {
    LazyColumn {
      items(notes.value.notes.size) { index ->
        Card(
          modifier = Modifier
            .padding(vertical = 8.dp)
            .fillMaxWidth(),
        ) {
          Row(
            modifier = Modifier.padding(16.dp),
            verticalAlignment = Alignment.CenterVertically,
          ) {
            Text(text = notes.value.notes[index].noteName)
            Text(text = notes.value.notes[index].noteBody)
          }
        }
      }
    }
    Row(
      modifier = Modifier
        .align(Alignment.BottomCenter)
        .padding(bottom = 16.dp),
    ) {
      Button(onClick = {
        coroutineScope.launch {
          noteVM.upsertNote(Note("Hello", "World"))
        }
      }) {
        Text(text = "Add")
      }
      Spacer(modifier = Modifier.width(16.dp))
      Button(onClick = {
        coroutineScope.launch {
          if (notes.value.notes.isEmpty()) {
            return@launch
          } else {
            noteVM.deleteNote(notes.value.notes.last())
          }
        }
      }) {
        Text(text = "Delete")
      }
    }
  }
}
